import {createUniFetch} from "uni-app-fetch"
import { useUserStore } from "../store/user"
const tabbarpath=[
	"pages/task/index",
	"pages/message/index",
	'pages/my/index'
]
export const uniFetch=createUniFetch({
	loading:{title:"正在加载..."},
	baseURL:"https://slwl-api.itheima.net",
	intercept:{
		request(options) {
			const userState=useUserStore()
			const defaltheader={
Authorization:userState.token				

			}
						// options.header=defaltheader
			options.header=Object.assign({},defaltheader,options.header)
			console.log(options.header,"header");
			return options
		},
		response(options) {
			console.log(options,'options');
			if(options.statusCode == 401){
				//页面栈
				const pageStack=getCurrentPages();
				console.log(pageStack,"pagestack");
				//当前页面地址
				const redirectURL=pageStack[pageStack.length-1].route;
				const routeType=tabbarpath.includes(redirectURL)?'switchTab':'redirectTo'
				return uni.redirectTo({
					url:`/pages/login/index?routeType=${routeType}&&redirectURL=${redirectURL}`
				})
				
			}
			return options
		}
	}
})
export const utils={
	toast(title = '数据加载失败',icon="none"){
		uni.showToast({
			title,
			icon,
			mask:true
		})
	}
}
uni.utils=utils;